<?php

namespace App\Http\Services;

use Illuminate\Support\Facades\DB;
use App\Model\SweetFriend;
use App\Model\Users;
use App\Model\RsRelationshipModel;
use App\Model\Api\RsSweetTitle;
use App\Model\SystemExplain;
use App\Model\Api\RsRelationshipTasks;
use App\Model\RsRelationshipIntimacyLog;

/**
 * 自定义游戏服务
 *
 */
class SweetFriendService
{
    protected $sweetFriend;
    protected $rsRelationshipModel;
    protected $couplingRing;
    protected $user;
    protected $rsSweetTitle;
    protected $relationshipIntimacyLog;
    protected $rsRelationshipTasks;
    public function __construct()
    {
        $this->user = new Users();
        $this->sweetFriend = new SweetFriend();
        $this->rsRelationshipModel = new RsRelationshipModel();
        $this->rsSweetTitle = new RsSweetTitle();
        $this->relationshipIntimacyLog = new RsRelationshipIntimacyLog();
        $this->rsRelationshipTasks = new RsRelationshipTasks();
    }


    public function getList ($page, $pageSize, $userId = 0) {
        $return = [];
        $offset = ($page - 1) * $pageSize;
        $rsRows = $this->rsRelationshipModel->getList($offset, $pageSize, 2, $userId);
        $total = $this->rsRelationshipModel->getCount(2, $userId);
        $rsCodes = array();
        $userIds = array();
        $friendIds = array();
        foreach ($rsRows as $v) {
            $vArr = explode('_', $v['rsCode']);
            $vArr[] = $v['rsCode'];
            $userIds[] = $vArr[2];
            $friendIds[] = $vArr[3];
            $rsCodes[] = $vArr;
        }

        $userData = $this->user->getUserInfoArrByIds(array_merge($userIds, $friendIds));
        $titles = $this->rsSweetTitle->getTitles();
        $userDataArr = array_column($userData ,null ,'user_id');
        $list = array();
        foreach ($rsCodes as $v) {
            $row = $this->sweetFriend->getList($v[2],$v[3]);
            if (isset($userDataArr[$v[2]])) {
                $row['userNickname'] = $userDataArr[$v[2]]['nickname'];
                $row['friendNickname'] = $userDataArr[$v[3]]['nickname'];
                $row['userUuid'] = $userDataArr[$v[2]]['uuid'];
                $row['friendUuid'] = $userDataArr[$v[3]]['uuid'];
                $row['userTitle'] = $titles[$row['titleId']]['title'];
                $row['friendTitle'] = $titles[$row['friendTitleId']]['title'];
                $row['rsCode'] = $v[4];
                $list[] = $row;
            }
        }
        $return['list'] = $list;
        $return['total'] = $total;
        $return['curPage'] = $page;
        return $return;
    }
    public function getIntimateList ($rsCode, $type) {
        $tasks = $this->rsRelationshipTasks->getTasks(2);
        $reasons = array_column($tasks, null, 'id');
        $list = $this->relationshipIntimacyLog->getLogs($rsCode, $type);
        foreach ($list as &$v) {
            if ($reasons[$v['taskId']]) {
                $v['reason'] = $reasons[$v['taskId']]['content'];
            } else {
                $v['reason'] = '';
            }
        }
        return $list;
    }
    public function getTitlelist () {
        return curl_request(getKkaudioApiUrl() . '/inner/sweetFriends/getTitleList', null,'urlencoded', 'GET');
    }

    public function createTitle ($data) {
        return curl_request(getKkaudioApiUrl() . '/inner/sweetFriends/createTitle', $data,'json', 'POST');
    }

    public function updateTitle ($data) {
        return curl_request(getKkaudioApiUrl() . '/inner/sweetFriends/updateTitle', $data,'json', 'POST');
    }

    public function deleteTitle ($data) {
        return curl_request(getKkaudioApiUrl() . '/inner/sweetFriends/deleteTitle', $data,'json', 'POST');
    }
}
